<script setup>
import { inject, ref } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { useaddressStore } from "@/stores/address";
import { addressListApi } from "@/services/address";
import { deliveryApi } from "@/services/cloudlibrary";
import { ordercheck, create } from "@/services/pay.js";
import { formatPrice } from "@/utils/formatPrice";

import { selectProductStore } from "@/stores/selectProduct";

const selectProductData = selectProductStore();

const dateobj = useaddressStore();

onLoad(() => {
  getaddressListFun();
});

onShow(() => {
  if (dateobj.address) {
    addressobj.value = JSON.parse(dateobj.address);
  }
  if (selectProductData.selectProduct) {
    goorderData.value = selectProductData.selectProduct;
  }
});

async function getaddressListFun() {
  //发送请求
  let res = await addressListApi({ page: 1 });
  if (res.code == 200) {
    addressobj.value = res.data.list[0] ? res.data.list[0] : "";
  }
}

const cartData = ref([]);

async function ordercheckfun() {
  goorderData.value.forEach((item, index) => {
    if (item.product_num > 0) {
      cartData.value.push(item);
    }
  });
  const res = await ordercheck({
    cart: cartData.value,
    address_id: addressobj.value.address_id,
    order_type: 0,
  });
}

const goorderData = ref([]);
const pickUpGoodData = ref({});
const remark = ref("");
const addressobj = ref({});

const showorder = ref(false);
const showtext = () => {
  showorder.value = true;
};

const closefun = () => {
  showorder.value = false;
};

// 备注确定
const configfun = () => {
  showorder.value = false;
};

// 提货
const productData = ref([]);
const goorderSubmit = async () => {
  if (numfun() <= 0) {
    uni.showToast({
      title: "商品数量不能为0,请选择",
      icon: "none",
    });
    return;
  }

  if (!addressobj.value) {
    uni.showToast({
      title: "用户地址不能为空",
      icon: "none",
    });
    return;
  }
  goorderData.value.forEach((item, index) => {
    if (item.product_num > 0) {
      productData.value.push(item);
    }
  });

  pickUpGoodData.value = {
    remark: remark.value,
    address_id: addressobj.value.address_id,
    product: productData.value,
  };
  try {
    // 发送请求
    let res = await deliveryApi(pickUpGoodData.value);
    if (res.code === 200) {
      // uni.navigateTo({
      //   url: "/pages/cloudlibrary/paystate/index?status=" + res.code,
      // });
      selectProductData.setselectProduct([]);
      uni.redirectTo({
        url: "/pages/cloudlibrary/paystate/index?status=" + res.code,
      });
    } else {
      uni.showToast({
        title: res.info,
        icon: "none",
      });
    }
  } catch (e) {
    uni.navigateTo({
      url: "/pages/cloudlibrary/paystate/index?status=" + e.code,
    });
  }
};

//地址
function addressListfun() {
  if (!addressobj.value) {
    uni.navigateTo({
      url: "/pages/address_add/index?type=add",
    });
  }
}
const goselectaddressfun = () => {
  uni.navigateTo({
    url: "/pages/address/index?type=add",
  });
};

//计算商品总数
function numfun() {
  let num = 0;
  if (goorderData.value.length > 0) {
    goorderData.value.forEach((element, index) => {
      if (element.selectStatus) {
        num += element.product_num;
      }
    });
    return num;
  } else {
    return num;
  }
}
</script>

<template>
  <view class="content">
    <view class="user-address">
      <view
        class="address-info-k"
        v-if="addressobj && addressobj.real_name"
        @click="goselectaddressfun"
      >
        <view class="address-info">
          <view class="address-info-name"
            ><text class="name">{{ addressobj.real_name }}</text
            ><text>{{ addressobj.phone }}</text></view
          >

          <view class="address-info-detail"
            >收货地址：{{
              addressobj.province +
              addressobj.city +
              addressobj.district +
              addressobj.detail
            }}</view
          >
        </view>
        <view>
          <up-icon name="arrow-right" color="#8C8C8C" size="32rpx"></up-icon>
        </view>
      </view>
      <view class="select-address" v-if="!addressobj" @click="addressListfun">
        <view class="select-address-add">
          <up-icon name="plus-circle" color="#01ADA8" size="24"></up-icon>
        </view>

        <view class="add-text">添加收货地址</view>
      </view>
    </view>
    <view class="goods">
      <view class="goods-shore">
        <text>已选商品</text>
        <text>共{{ numfun() }}件</text>
      </view>
      <view
        class="goods-info"
        v-for="(item, index) in goorderData"
        :key="index"
      >
        <view class="goods-info-img">
          <image :src="item.product.image" mode="aspectFill" />
        </view>
        <view class="goods-info-detail">
          <view class="goods-info-detail-name">
            <view class="goods-name">{{ item.product.store_name }}</view>
          </view>
          <view class="goods-priceornum">
            <view class="goods-price"
              >会员价:<text>
                ¥{{ formatPrice(item.product.vip_price) }}</text
              ></view
            >
            <view class="goods-num">
              <up-number-box
                integer
                :min="0"
                :max="item.quantity"
                bgColor="#ffffff"
                v-model="item.product_num"
              ></up-number-box>
            </view>
          </view>
        </view>
      </view>
      <view class="goods-paylist">
        <view class="goods-paylist-item">
          <view class="goods-paylist-item-left">订单备注</view>
          <view class="goods-paylist-item-righ" @click="showtext"
            ><text
              class="color-select-mr"
              :class="remark ? 'color-select-mr-selet' : ''"
              >{{ remark || "无备注" }}</text
            >
            <up-icon name="arrow-right" color="#8C8C8C" size="32rpx"></up-icon
          ></view>
        </view>
      </view>
    </view>
    <view class="goods-paylist goods-paylist-item-k">
      <view class="goods-paylist-item-k-title">提货详情</view>
      <view class="goods-paylist-item">
        <view class="goods-paylist-item-left">配送方式</view>
        <view class="goods-paylist-item-righ"
          ><text class="color-select">快递配送</text>
        </view>
      </view>
      <view class="goods-paylist-item">
        <view class="goods-paylist-item-left">运费</view>
        <view class="goods-paylist-item-righ">免运费</view>
      </view>
    </view>
  </view>

  <view class="foodr-bt">
    <view class="bt-sbuit" @click="goorderSubmit()">确定提货</view>
  </view>

  <up-popup
    round="10"
    :show="showorder"
    @close="closefun"
    @cancel="closefun"
    @open="open"
  >
    <view class="popup-content">
      <view class="popup-content-bt">
        <text class="qx" @click="closefun">取消</text>
        <text>订单备注</text>
        <text class="qd" @click="configfun">确定</text>
      </view>
      <view class="popup-content-text">
        <up-textarea
          v-model="remark"
          placeholder="请输入内容"
          count
          height="262rpx"
        ></up-textarea>
      </view>
    </view>
  </up-popup>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>
